﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using Logging;

namespace SelectionAddin.Extension.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        public UnitTest1()
        {
            string logFile = "s:\\log\\" + "CompleterTester"
                + "." + DateTime.Now.ToString("s").Replace(":", "")
                + ".txt";
            Logger.SetOutputFile(File.Open(logFile, FileMode.Create));
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        // Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup()]
        public static void MyClassCleanup()
        {
            Logger.Close();
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        protected void RunTest(CompleterRankBoundTestInstance instance) {
            bool passed = instance.Test.RunTest();
            if (!passed)
            {
                throw new Exception("Test failed.");
            }
        }

        [TestMethod]
        public void HelloWorld()
        {
            RunTest(new CompleterRankBoundTestInstance("HelloWorld",
                @"using System;
    
                    namespace HelloWorld
                    {
                        class Program
                        {
                            static void Main(string[] args)
                            {
                                /*^*/m(""Hello, World!"")/*$*/
                            }
                        }
                    }",
                @"System.Console.WriteLine(""Hello, World!"")", 10));
        }

        [TestMethod]
        /// Tests abstract type inference within Roslyn.
        public void AbstractTypesRoslyn()
        {
            var code = @"using System;
    
                    namespace HelloWorld
                    {
                        class Program
                        {
                            static void Main(string[] args)
                            {
                                Program p = new Program();
                                /*^*/p.m(p.f)/*$*/
                            }

                            public string Fontname { get { return ""name""; } }
                            public string Filename { get { return ""name""; } }
                            public void UseFile(string filename) { }
                            public void UseFont(string fontname) { }

                            public void TestCase()
                            {
                                UseFile(Filename);
                                UseFont(Fontname);
                            }
                        }
                    }";
            RunTest(new CompleterRankBoundTestInstance("AbstractTypesRoslyn-File",
                code, "p.UseFile(p.Filename)", 2));
            RunTest(new CompleterRankBoundTestInstance("AbstractTypesRoslyn-Font",
                code, "p.UseFont(p.Fontname)", 2));
        }
    }
}
